package com.leilingmin.www.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leilingmin.www.entity.Illness;
import com.leilingmin.www.entity.IllnessKind;
import com.leilingmin.www.entity.PageView;
import com.leilingmin.www.mapper.IllnessKindMapper;
import com.leilingmin.www.mapper.IllnessMapper;
import com.leilingmin.www.mapper.PageViewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IllnessService extends ServiceImpl<IllnessMapper, Illness> {
    @Autowired
    private IllnessKindMapper illnessKindMapper;
    @Autowired
    private IllnessKindService illnessKindService;
    @Autowired
    private PageViewMapper pageViewMapper;
    @Autowired
    private PageViewService pageViewService;
    public IPage<Illness> getIllnessPageDataByIllnessName(String illnessName, int pageSize, int curPage){
        Page<Illness> illnessPage = new Page<Illness>(curPage,pageSize);
        QueryWrapper<Illness> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(illnessName!=null,"illness_name",illnessName);
        IPage<Illness> illnessIPage = this.page(illnessPage,queryWrapper);
        List<Illness> records=illnessPage.getRecords();
        if (illnessIPage.getRecords().size()>0){
            records.forEach(s->{
                s.setKindName(illnessKindMapper.selectById(s.kindId).getName());
                QueryWrapper<PageView> wrapper=new QueryWrapper<>();
                wrapper.eq("illness_id",s.id);
                s.setPageView(pageViewMapper.selectOne(wrapper).getPageViews());
            });
        }
        records.forEach(System.out::println);
        return illnessIPage;

    }
//    public List<Illness> getIllnessPageDate(Integer currentPage, Integer pageSize){
//
//        Page<Illness> illnessPage = new Page<Illness>(currentPage,pageSize);
//
//        List<Illness> illnessList =this.page(illnessPage).getRecords();
//
//        if (illnessList.size() > 0) {
//
//            illnessList.forEach(s -> {
//
//                s.setKindName(illnessKindService.getById(s.getKindId()).getName());
//
//                QueryWrapper queryWrapper1 = new QueryWrapper<>();
//
//                queryWrapper1.eq("illness_id",s.getId());
//
//                s.setPageView(pageViewService.getOne(queryWrapper1).getPageViews());
//
//            });
//
//        }
//
//        return illnessList;
//
//    }
}
